package cn.zhiwen.consumerController;

import cn.zhiwen.entity.RegisterData;
import cn.zhiwen.entity.TokenData;
import cn.zhiwen.entity.User;
import cn.zhiwen.service.HelloService;
import cn.zhiwen.service.UserCommand;
import com.google.gson.Gson;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("springConsumer01")
public class ConsumerController {
    @Autowired
    private HelloService helloService;
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/ribbon-sonsumer")
    public String helloConsumer() {
        //return restTemplate.getForEntity("http://springClient01/hello",String.class).getBody();
        return helloService.helloService();
    }

    @RequestMapping("/user")
    public Object userConsumer() throws Exception {
        //return restTemplate.getForEntity("http://springClient01/hello",String.class).getBody();
        //t同步执行
        //return new UserCommand().execute();
        //异步执行
        return new UserCommand(restTemplate, 21L).queue();
    }

    @RequestMapping(value = "/registerUser")
    public String registerUser() {
//        String url = "https://a1.easemob.com/" + RegisterData.getOrg_name() + "/" + RegisterData.getApp_name() + "/users";
        //String urls = "https://a1.easemob.com/1169180730177231/wuzhiwen-demo/users";
        String urls = "https://a1-vip5.easemob.com/1437180817061664/kefuchannelapp56808/users";
        Map<String, String> users = new HashMap<String, String>();
        users.put("username", "wuzhiwen"+ new Random().nextInt());
        users.put("password", "wuzhiwen");
        Gson gson = new Gson();
        String s = gson.toJson(users);
        HttpHeaders headers = new HttpHeaders();
//        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
//        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Authorization", "Bearer " + getToken());

//        HttpEntity<Map> formEntity = new HttpEntity<Map>(users, headers);
        HttpEntity<String> formEntity = new HttpEntity<String>(s, headers);
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(urls, formEntity, String.class);
        return stringResponseEntity.getBody().toString();
    }

    @RequestMapping(value = "/getToken")
    public String getToken() {
        String urls = "https://a1-vip5.easemob.com/1437180817061664/kefuchannelapp56808/token";
//        String urls = "https://a1.easemob.com/1169180730177231/wuzhiwen-demo/token";
        Map<String, String> token = new HashMap<String, String>();
        token.put("grant_type", "client_credentials");
//        token.put("client_id", "YXA6OJ7_EJPDEeiJvFMDfKZe6g");
        token.put("client_id", "YXA6KKPwQKHCEeioudNczvTu9Q");

//        token.put("client_secret", "YXA6BXlsbsV3L0IOcObE2KaRPzihPo0");
        token.put("client_secret", "YXA6172VHRTgLaRkr6t240oPRSDIl0Q");
        token.put("limit", "5");

        Gson gson = new Gson();
        String s = gson.toJson(token);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity<String> formEntity = new HttpEntity<String>(s, headers);

        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(urls, formEntity, String.class);
        String body = stringResponseEntity.getBody();
        TokenData tokenData = gson.fromJson(body, TokenData.class);
        return tokenData.getAccess_token();
    }

    @RequestMapping(value = "/deleteUser")
    public String deleteUser() {
        String urls = "https://a1.easemob.com/1169180730177231/wuzhiwen-demo/users";

        HttpHeaders headers = new HttpHeaders();

        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Authorization", "Bearer " + getToken());

        HttpEntity<String> formEntity = new HttpEntity<String>(null, headers);
        ResponseEntity<String> exchange = restTemplate.exchange(urls + "/wuzhiwen", HttpMethod.DELETE, formEntity, String.class);
        return exchange.getBody();
    }

    @RequestMapping(value = "/deleteSetBody",method = RequestMethod.DELETE)
    public String deleteWay(@RequestParam("body") String body){

        return body;
    }

}
